package com.diver.flinkdemo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.diver.flinkdemo.entity.xxt.LocationInfo;
import com.diver.flinkdemo.mapper.dorisMapper.LocationInfoMapper;
import com.diver.flinkdemo.service.ILocationInfoService;
import com.diver.flinkdemo.utils.DynamicDataSourceUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSession;

/**
 * Created with IntelliJ IDEA.
 *
 * @Description:
 * @Author: zhangB
 * @Date: 2023/2/24 13:45
 */

@Slf4j
//@Service
public class LocationInfoServiceImpl implements ILocationInfoService {

//    @Autowired
    private LocationInfoMapper locationInfoMapper ;
    SqlSession session = null;

    @Override
    public LocationInfo getPreLocationInfo(long timestamp, String studentCardId) {
        try {
            session = DynamicDataSourceUtil.dorisSessionFactory.openSession(true);
            locationInfoMapper = session.getMapper(LocationInfoMapper.class);
            LocationInfo preLocationInfo = locationInfoMapper.getPreLocationInfo(timestamp, studentCardId);
            return preLocationInfo;
        } finally {
//            endOfSession(session);
        }
    }

    @Override
    public LocationInfo getByParams(QueryWrapper<LocationInfo> wrapper) {
        try {
            session = DynamicDataSourceUtil.dorisSessionFactory.openSession();
            locationInfoMapper = session.getMapper(LocationInfoMapper.class);
            return locationInfoMapper.selectOne(wrapper);
        } finally {
            endOfSession(session);
        }
    }


    public void endOfSession(SqlSession session) {
        //关闭sqlSession
        if (session != null) {
            log.info("session commit -> session close");
//            session.commit();
            session.close();

        }
    }
}
